package com.example.yudaoly.security;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yudaoly.domain.pojo.Users;
import com.example.yudaoly.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


@Component
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UsersService usersService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",s);
        //根据用户名取数据库查询用户
        Users user = usersService.getOne(queryWrapper);
        if(user == null){
            throw new UsernameNotFoundException(s);
        }else {
            return new LoginUser(user,null);
        }
    }
}
